//======================================================================================
//  Notes:Use this command to anonymously check out the latest project source code:
//  Non-members may check out a read-only working copy anonymously over HTTP.
//  http://YHQCodingWsp.googlecode.com/svn/trunk/
//
//  Author:		Kevin Yang
//  Email:		Bigearrabbit@qq.com
//
//  DATE:		2013-4-16
//
//  File Name:	YHQDockWidgetCAATools.cpp
//
//  Notes:
//
//======================================================================================

//local Library
#include "YHQDockWidgetCAATools.h"
#include "ui_YHQDockWidgetCAATools.h"

//YHQCodingCore library
#include "YHQSvnFolderAdd.h"

//Qt Library
#include <QDir>
#include <QStringList>

//Append to list macro
#define LocalAppendToListOfFolders(iName) \
    row.Name = iName; \
    ListOfFolders.append(row)

//-----------------------------------------------------
YHQDockWidgetCAATools::YHQDockWidgetCAATools(QWidget *parent)
    : YHQDockWidget(parent)
    , ui(new Ui::YHQDockWidgetCAATools)
{
    ui->setupUi(this);
    this->InitialFolderList();//initial

}
//-----------------------------------------------------
YHQDockWidgetCAATools::~YHQDockWidgetCAATools()
{
    delete ui;//release
}
//-----------------------------------------------------
bool YHQDockWidgetCAATools::DeleteFolderContents(const QString & iFolderPath,
                                                 const QStringList & iFolderDelete,
                                                 const QStringList & iFolderIgnore)
{
    //out put msg
    OutPutMessage("=====Bigain Delete Folder Containts=====");//out put message

    //get folder
    OutPutMessage(" >>--- Begin Delete Contents.Folder is :" + iFolderPath + "---------");//out put message

    YHQFolderCAAOperate caaOperate; //for operate
    caaOperate.SetOutsideTextEditPtr(m_pOutsideTextEdit);//set tex edit
    caaOperate.SetListOfDelete(iFolderDelete);//set list
    caaOperate.SetListOfIgnore(iFolderIgnore);//set list

    //delete no use contents
    bool isOK = caaOperate.DeleteNoUseContents(iFolderPath);

    //out put message control
    QString stringResult = isOK?"OK.":"Failed:";//string for result
    OutPutMessage(" <<---Delete Folder" + stringResult);//out put message
    OutPutMessage("=====Finished Delete Folder Containts====");//out put message
    return true;
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::InitialFolderList(QVector<YHQ::FileInforsRow> & iListOfFolders,
                                              QListWidget *ipListWidget)
{
    // add to list
    for(int i=0;i<iListOfFolders.size();i++)
    {
        YHQ::FileInforsRow & row = iListOfFolders[i];//get row
        QListWidgetItem * plistwidgetitem = new QListWidgetItem(ipListWidget);
        plistwidgetitem->setCheckState(row.CheckState);//set checked
        plistwidgetitem->setText(row.Name);//set name
    }
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::InitialFolderList()
{
    YHQ::FileInforsRow row;//row instance
    QVector<YHQ::FileInforsRow> ListOfFolders;//FolderList

    //For Delete list
    {
        ListOfFolders.clear();//clear first
        //Checked list
        row.CheckState = Qt::Checked;//Checked
        LocalAppendToListOfFolders("ImportedInterfaces");//apend new row
        LocalAppendToListOfFolders("LocalGenerated");//apend new row
        LocalAppendToListOfFolders("ProtectedGenerated");//apend new row
        LocalAppendToListOfFolders("Objects");//apend new row
        LocalAppendToListOfFolders("DbcsTest");//apend new row
        LocalAppendToListOfFolders("NlsTest");//apend new row

        //Unchecked list
        row.CheckState = Qt::Unchecked;//Unchecked
        LocalAppendToListOfFolders("ToolsData");//apend new row
        LocalAppendToListOfFolders("intel_a");//apend new row
        LocalAppendToListOfFolders("win_b64");//apend new row

        // add to list
        InitialFolderList(ListOfFolders,ui->listWidgetFolderDelete);
    }

    //for Ignore
    {
        //Checked list
        ListOfFolders.clear();//clear first
        row.CheckState = Qt::Checked;//Checked
        LocalAppendToListOfFolders(".svn");//apend new row
        LocalAppendToListOfFolders("various");//apend new row

        //Unchecked list
        row.CheckState = Qt::Unchecked;//Unchecked

        // add to list
        InitialFolderList(ListOfFolders,ui->listWidgetFolderIgnore);
    }

    //for SVN Ignore
    {
        //Checked list
        ListOfFolders.clear();//clear first
        row.CheckState = Qt::Checked;//Checked
        LocalAppendToListOfFolders("ImportedInterfaces");//apend new row
        LocalAppendToListOfFolders("LocalGenerated");//apend new row
        LocalAppendToListOfFolders("ProtectedGenerated");//apend new row
        LocalAppendToListOfFolders("ToolsData");//apend new row
        LocalAppendToListOfFolders("Objects");//apend new row
        LocalAppendToListOfFolders("DbcsTest");//apend new row
        LocalAppendToListOfFolders("NlsTest");//apend new row        LocalAppendToListOfFolders("toolsdata");//apend new row
        LocalAppendToListOfFolders("intel_a");//apend new row
        LocalAppendToListOfFolders("win_b64");//apend new row
        LocalAppendToListOfFolders("various");//apend new row

        //Unchecked list
        row.CheckState = Qt::Unchecked;//Unchecked

        // add to list
        InitialFolderList(ListOfFolders,ui->listWidgetSvnIgnore);
    }
}
//-----------------------------------------------------
bool YHQDockWidgetCAATools::MoveSelectedItems(QListWidget * ipListWidgetSource,
                                              QListWidget * ipListWidgetTaret)
{
    if(!ipListWidgetSource || !ipListWidgetTaret)
    {
        OutPutMessage("Input List Widget is NULL");//out put message
        return false;
    }

    //Get Index

    QItemSelectionModel * selModel =  ipListWidgetSource->selectionModel();//

    QModelIndexList indexList = selModel->selectedIndexes();//
    QMap<int,int> rowsMap;//record rows,this will sort auto

    //Record all
    for (int i=0;i<indexList.size();i++)
    {
        rowsMap.insert(indexList[i].row(),0);//inset row
    }


    QListWidgetItem * pItem = NULL;//get item

    int row = -1;//current row

    QMapIterator<int,int> iter(rowsMap);//instance a iter
    iter.toBack();//to back
    //loop form back
    while(iter.hasPrevious())
    {
        iter.previous();//move to previous

        row = iter.key();//get row index
        pItem = ipListWidgetSource->takeItem(row);//take off
        ipListWidgetTaret->addItem(pItem);//add
    }
    return true;//ok

}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteSvn_clicked()
{
    //Delete list set
    QStringList listOfDelete;//for delete
    listOfDelete.append(".svn");//add lower folder name

    //Ignore list set
    QStringList listOfIgnore;//for Ignore
    //listOfIgnore.append(".svn");//add lower folder name

    //call delete function
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         listOfDelete,
                         listOfIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteToolsData_clicked()
{
    //Delete list set
    QStringList listOfDelete;//for delete
    listOfDelete.append("toolsdata");//add lower folder name

    //Ignore list set
    QStringList listOfIgnore;//for Ignore
    listOfIgnore.append(".svn");//add lower folder name

    //call delete function
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         listOfDelete,
                         listOfIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteDebugAndRelease_clicked()
{
    //Delete list set
    QStringList listOfDelete;//for delete
    listOfDelete.append("debug");//add lower folder name
    listOfDelete.append("release");//add lower folder name

    //Ignore list set
    QStringList listOfIgnore;//for Ignore
    listOfIgnore.append(".svn");//add lower folder name

    //call delete function
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         listOfDelete,
                         listOfIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteCAAExportFolder_clicked()
{
    //Delete list set
    QStringList listOfDelete;//for delete
    listOfDelete.append("intel_a");//add lower folder name
    listOfDelete.append("win_b64");//add lower folder name

    //Ignore list set
    QStringList listOfIgnore;//for Ignore
    listOfIgnore.append(".svn");//add lower folder name

    //call delete function
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         listOfDelete,
                         listOfIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteCAAFolder_clicked()
{
    this->UpdateListOfFolders();//update list
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         m_ListOfDelete,
                         m_ListOfIgnore);//call delete function
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonDeleteObjects_clicked()
{
    //Delete list set
    QStringList listOfDelete;//for delete
    listOfDelete.append("objects");//add lower folder name

    //Ignore list set
    QStringList listOfIgnore;//for Ignore
    listOfIgnore.append(".svn");//add lower folder name

    //call delete function
    DeleteFolderContents(this->ui->lineEditFolderPath->text(),
                         listOfDelete,
                         listOfIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonSvnInitialAdd_clicked()
{

    //out put msg
    OutPutMessage("=====Bigain SvnInitialAdd Folder Containts=====");//out put message

    QString FolderPath = this->ui->lineEditFolderPath->text();//get path
    QStringList ListOfIgnore;
     //Get Ignore List
    {
        ListOfIgnore.clear();//clear first
        ListOfIgnore.append(".svn");
        QListWidget * pListWidget = ui->listWidgetSvnIgnore;
        const int count = pListWidget->count();

        //loop to get Delete list
        for(int i=0;i<count;i++)
        {
            QListWidgetItem * pItem =pListWidget->item(i);
            if (!pItem) continue;//check pointer
            if (Qt::Checked != pItem->checkState()) continue;//not checked
            ListOfIgnore.append( pItem->text().toLower());
        }

    }

    //get folder
    OutPutMessage(" >>--- Begin SvnInitialAdd Contents.Folder is :" + FolderPath + "---------");//out put message

    YHQSvnFolderAdd svnAddOpt; //for operate
    svnAddOpt.SetOutsideTextEditPtr(m_pOutsideTextEdit);//set tex edit
    //caaOperate.SetListOfDelete(iFolderDelete);//set list
    svnAddOpt.SetListOfIgnore(ListOfIgnore);//set list

    //delete no use contents
    bool isOK = svnAddOpt.SvnInitialAddTop(FolderPath);

    //out put message control
    QString stringResult = isOK?"OK.":"Failed:";//string for result
    OutPutMessage(" <<---SvnInitialAdd Folder" + stringResult);//out put message
    OutPutMessage("=====Finished SvnInitialAdd Folder Containts====");//out put message
    return ;

}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonMoveToRight_clicked()
{
    //move
    MoveSelectedItems(ui->listWidgetFolderDelete,
                      ui->listWidgetFolderIgnore);
}
//-----------------------------------------------------
void YHQDockWidgetCAATools::on_pushButtonMoveToLeft_clicked()
{
    //move
    MoveSelectedItems(ui->listWidgetFolderIgnore,
                      ui->listWidgetFolderDelete);

}
//-----------------------------------------------------
void YHQDockWidgetCAATools::UpdateListOfFolders()
{
    //Get Delete List
    {
        m_ListOfDelete.clear();//clear first
        QListWidget * pListWidget = ui->listWidgetFolderDelete;
        const int count = pListWidget->count();

        //loop to get Delete list
        for(int i=0;i<count;i++)
        {
            QListWidgetItem * pItem =pListWidget->item(i);
            if (!pItem) continue;//check pointer
            if (Qt::Checked != pItem->checkState()) continue;//not checked
            m_ListOfDelete.append( pItem->text().toLower());
        }

    }

    //Get Ignore List
    {
        m_ListOfIgnore.clear();//clear first
        QListWidget * pListWidget = ui->listWidgetFolderIgnore;
        const int count = pListWidget->count();

        //loop to get Delete list
        for(int i=0;i<count;i++)
        {
            QListWidgetItem * pItem =pListWidget->item(i);
            if (!pItem) continue;//check pointer
            if (Qt::Checked != pItem->checkState()) continue;//not checked
            m_ListOfIgnore.append( pItem->text().toLower());
        }

    }

}
//-----------------------------------------------------

